import entity
import pygame
import helpers
import xml.etree.cElementTree as ET

class Structure(entity.Entity):

    # Used for keeping track of the time in an animation
    animation_timer = 0
    
    # Used for keeping track of when a structure is finshed building
    build_timer = 0
    
    # Play a single animation for this long. Currently controls the blinking
    # time when building a structure.
    ANIMATION_INTERVAL = 0.5

    # Current state of the structure.
    # Values: Build, Ready
    state = None

    # Reference to the player object
    player = None

    # Add mass/energy to the player
    mass_regen = 0
    energy_regen = 0

    def __init__(self, structure_name, player):
        entity.Entity.__init__(self)
        
        self.player = player
        self.load_structure_data(structure_name)
        
        self.image, self.rect = helpers.load_image(self.image_name, -1)
        
        self.state = "Build"
        
    def update(self, ship_list, dtime):
        '''
        Handles all calculations and such.
        '''
        # Check if we are done building the tower
        if self.state == 'Build':
            self.build_timer += dtime
            if self.build_timer > self.build_time:
                self.state = 'Ready'
                return

        self.player.energy_current += self.energy_regen * dtime
        self.player.mass_current   += self.mass_regen   * dtime

    def draw(self, viewport, dt):
        '''
        Draw the structure to the screen. Calls the necessary sub-drawing
        functions.
        '''
        if self.state == "Build":
            self.draw_build(viewport, dt)
        elif self.state == "Ready":
            self.draw_ready(viewport, dt)
            
    def draw_build(self, viewport, dt):
        '''
        Draw a structure being built to the screen.
        '''
        self.animation_timer += dt
        if self.animation_timer > self.ANIMATION_INTERVAL:
            viewport.screen.blit(self.image, self.rect)
        if self.animation_timer > self.ANIMATION_INTERVAL*2:
            self.animation_timer = 0
    
    def draw_ready(self, viewport, dt):
        '''
        Draw a built structure to the screen.
        '''
        viewport.screen.blit(self.image, self.rect)
        
    def set_rect_center(self, center):
        '''                
        Used for setting the center position. Duplicate of similar method in
        Tower. Should be removed in future versions. TODO.
        '''
        self.position = center
        self.rect.center = center        
        
    def load_structure_data(self, structure_name):
        '''
        Loads the structures data from the corresponding XML-file
        '''
        structure_name = "../data/structures/" + structure_name + ".xml"
        tree = ET.ElementTree(file=structure_name)
        
        self.name        = tree.find('name').text
        self.description = tree.find('description').text
        self.image_name  = tree.find('image').text
        self.health      = int( tree.find('health').text )
        self.mass_cost   = int( tree.find('buildcost/mass').text )
        self.energy_cost = int( tree.find('buildcost/energy').text )
        self.build_time  = int( tree.find('buildtime').text )
        
        node = tree.find('abilities/energy_regen')
        if ET.iselement(node):
            self.energy_regen = int( node.text )

        node = tree.find('abilities/mass_regen')
        if ET.iselement(node):
            self.mass_regen = int( node.text )
            
        # Make a copy of some of the variables
        self.max_health = self.health        